﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SkynetCE.Foundation.Constants;
using SkynetCE.Foundation.Services.Navigation;
using SkynetCE.Administration.Views.MgmCategories;
using SkynetCE.Administration.Views.MgmExams;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
using SkynetCE.Foundation.Services.DatabaseService;
using Microsoft.WindowsMobile.Status;
using Infrastructure.CompactCE.ExceptionHandling;   
namespace SkynetCE.Shell
{
    public partial class Shell : Form
    {
    
        public IDatabaseService DatabaseService { get; set; }  

        public Shell()
        {
            InitializeComponent();
           
        }
        private void menuHelp_Click(object sender, EventArgs e)
        {
            var frm = new HelpForm();
            frm.Owner = this; 
            frm.ShowDialog();  
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {

        }

        private void menuCategories_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate<IMgmCategoriesView>(RegionNames.ModalRegion, this);  
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate<IMgmExamView>(RegionNames.ModalRegion, this);
        }

       
       



        private void Shell_Load(object sender, EventArgs e)
        {

            
            //this.lblWelcome.Text = "Welcome " + SystemState.GetValue(SystemProperty.HeadsetPresent).ToString();
            //this.lblBatery.Text = SystemState.GetValue(SystemProperty.PowerBatteryStrength).ToString()  + "%"; 
        }
        private void menuLoad_Click(object sender, EventArgs e)
        {
            try
            {
                this.DatabaseService = NavigationService.ResolveService<Foundation.Services.DatabaseService.IDatabaseService>();
                var ceRDA = new SqlCeRemoteDataAccess();
                ceRDA.InternetLogin = string.Empty;
                ceRDA.InternetPassword = string.Empty;
                ceRDA.InternetUrl = "http://devmobile/SyncWebSite/sqlcesa35.dll";
                string localDbConnectionString = string.Empty;
                CreateLocalDb(ref localDbConnectionString);
                ceRDA.LocalConnectionString = localDbConnectionString;
                string _oleConnectionString = this.DatabaseService.OleDbServerConnectionString;
                ceRDA.Pull("ExamCategories", "select * from ExamCategories", _oleConnectionString, RdaTrackOption.TrackingOn);
                this.menuSave.Enabled = true;
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.SmartViewPolicy);
                if (rethrow)
                    throw;                 
            }
            
        }
        private void CreateLocalDb(ref string localPath)
        {
            SqlCeEngine engineCe = new SqlCeEngine();

            //Path de la base de datos
            string pathDB = string.Empty;
            pathDB = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            pathDB = Path.Combine(pathDB, "SkynetLibrary.sdf");
            
            //Creamos la base de datos vacía
            if (File.Exists(pathDB) )
            {
                File.Delete(pathDB);
            }
            
            localPath = string.Format("Data Source={0}", pathDB);
            engineCe.LocalConnectionString = localPath;
            engineCe.CreateDatabase();
        }
        private void menuSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.DatabaseService = NavigationService.ResolveService<Foundation.Services.DatabaseService.IDatabaseService>();
                var ceRDA = new SqlCeRemoteDataAccess();
                ceRDA.InternetLogin = string.Empty;
                ceRDA.InternetPassword = string.Empty;
                ceRDA.InternetUrl = "http://devmobile/SyncWebSite/sqlcesa35.dll";
                
                string pathDB = string.Empty;
                pathDB = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                pathDB = Path.Combine(pathDB, "SkynetLibrary.sdf");
                string localDbConnectionString = string.Format("Data Source={0}", pathDB);

                ceRDA.LocalConnectionString = localDbConnectionString;
                string _oleConnectionString = this.DatabaseService.OleDbServerConnectionString;
                ceRDA.Push("ExamCategories", _oleConnectionString, RdaBatchOption.BatchingOn);
                this.menuSave.Enabled = true;
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.SmartViewPolicy);
                if (rethrow)
                    throw;
            }
        }

        private void menuCategoriesMerge_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate<Administration.Views.MgmCategoriesMerge.IMgmCategoriesView>(RegionNames.ModalRegion, this);  
        }       
    }
}